/**
 * Created by LeungZ on 2016/3/22.
 */
import './navigation.scss';
import temp from './navigation.html';

class NavigationDrct {
    constructor() {
        this.replace = true;
        this.restrict = 'E';
        this.scope = {};
        this.templateUrl = temp;
        this.controllerAs = 'nav';
    }

    link(scope, element, attr, ctrl) {
        element.delegate('li > span', 'click', function () {
            if (element.find(this).find('i').hasClass('fa-caret-down')) {//已经展开二级菜单
                element.find(this).find('i').removeClass('fa-caret-down').addClass('fa-caret-right')
                    .end().next().height(0);
            } else {//未展开二级菜单
                element.find(this).find('i').removeClass('fa-caret-right').addClass('fa-caret-down')
                    .end().next().height(ctrl.getHeight(element.find(this).parent()));
            }
        });

        scope.$on('$updateNavMenu', function () {
            if (ctrl.links) {
                if (ctrl.setDbCode()) {
                    element.find('li > a[href="#/k8s"]').addClass('opened').next()
                        .height(ctrl.getHeight(element.find('li > a[href="#/k8s"]').parent()));
                } else element.find('li > a[href="#/k8s"]').removeClass('opened').next().height(0);

                if (element.find('li.actived').removeClass('actived').end()
                        .find('li > a[tag="#' + ctrl._location.path().split('/').splice(0, 3).join('/') + '"]')
                        .parent('li').addClass('actived').parent('ul').prev().find('i').hasClass('fa-caret-right')) {
                    element.find('li.actived').parent('ul').prev().trigger('click');
                }
            }
        });
    }

    controller($location, $rootScope, $stateParams, userSrv, uiNotification) {
        "ngInject";
        this._location = $location;

        userSrv.getMenu().then(data=> {
            this.links = data.menus;
            $rootScope.$applyAsync(function () {
                $rootScope.$broadcast('$updateNavMenu');
            });
        }).catch(function (data) {
            uiNotification.error({
                title: '菜单信息',
                message: '获取菜单信息数据有误'
            });
        });

        this.toggleNav = function () {
            $rootScope.hideNav = !$rootScope.hideNav;
        };
        this.setDbCode = ()=> {
            for (var i in this.links) {
                if (this.links[i].urlAddr == '#/k8s') {
                    angular.forEach(this.links[i].item, link=> {
                        link.urlAddr = link.urlAddr.split('?')[0] +
                            ($stateParams.kDbCode ? ('?kDbCode=' + $stateParams.kDbCode) : '');
                    });
                    return $stateParams.kDbCode;
                }
            }
        };

        this.getHeight = function (li) {
            let ulHeight = 10; // ul total padding
            angular.forEach(li.find('ul > li'), obj => {
                ulHeight += obj.clientHeight;
            });
            return ulHeight;
        };
    }

    static factory() {
        return new NavigationDrct();
    }
}
export default NavigationDrct